﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using MicropleSoft.Controls;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.Proxy;
using MicropleSoft.WmsApp.Library.Security;
using MicropleSoft.Controls.Style;

namespace MicropleSoft.WmsApp.Base.Window
{
    public partial class BaseForm : XtraForm, IBaseForm
    {
        protected readonly ServerProxy ServerProxy = null;

        private readonly string _openThisFormUserId = "notRuntime";
        /// <summary>
        /// 打开此窗口的用户ID
        /// </summary>
        public string OpenThisFormUserId
        {
            get { return _openThisFormUserId; }
        }

        #region RepositoryItem 4个

        readonly RepositoryItemTextEdit _repTextEdit0Dot00 = null;
        protected RepositoryItem RepTextEdit0Dot00
        {
            get { return _repTextEdit0Dot00; }
        }

        readonly RepositoryItemTextEdit _repTextEdit0Dot0000 = null;
        protected RepositoryItem RepTextEdit0Dot0000
        {
            get { return _repTextEdit0Dot0000; }
        }

        /// <summary>
        /// 获取RepositoryItem，有小数位数
        /// </summary>
        /// <param name="decimalPlaces">小数位数</param>
        /// <returns></returns>
        public RepositoryItem GetRepositoryItem0DotDecimalPlaces(int? decimalPlaces)
        {
            if (decimalPlaces > 10)
            {
                throw new Exception("小数位数不能大于10！");
            }
            const string mask = "0.0000000000";
            var repTextEdit0Dotx = new RepositoryItemTextEdit();
            InitControl(repTextEdit0Dotx, "n");

            repTextEdit0Dotx.DisplayFormat.FormatString = mask.Substring(0, 2 +
                                                                         (decimalPlaces == null ? 2 : (int)decimalPlaces)
                                                                         );
            repTextEdit0Dotx.EditFormat.FormatString = repTextEdit0Dotx.DisplayFormat.FormatString;
            return repTextEdit0Dotx;
        }

        /// <summary>
        /// 获取RepositoryItem，有文本长度
        /// </summary>
        /// <param name="maxLength">文本长度</param>
        /// <returns></returns>
        public RepositoryItem GetRepositoryItemText(int? maxLength)
        {
            var repTextEdit = new RepositoryItemTextEdit();
            ((System.ComponentModel.ISupportInitialize)(repTextEdit)).BeginInit();
            repTextEdit.AutoHeight = false;
            repTextEdit.DisplayFormat.FormatType = DevExpress.Utils.FormatType.None;
            repTextEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.None;
            repTextEdit.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None;
            repTextEdit.Mask.UseMaskAsDisplayFormat = true;
            if (maxLength != null)
            {
                repTextEdit.MaxLength = (int)maxLength;
            }
            ((System.ComponentModel.ISupportInitialize)(repTextEdit)).EndInit();
            return repTextEdit;
        }

        /// <summary>
        /// 获取RepositoryItemCheckEdit
        /// </summary>
        /// <returns></returns>
        public RepositoryItem GetRepositoryItemCheckEdit()
        {
            var repTextEdit = new RepositoryItemCheckEdit();
            repTextEdit.CheckStyle = CheckStyles.Standard;
            return repTextEdit;
        }

        /// <summary>
        /// 获取日期RepositoryItemDate
        /// </summary>
        /// <returns></returns>
        public RepositoryItem GetRepositoryItemDate()
        {
            var repTextEdit = new RepositoryItemDateEdit();
            return repTextEdit;
        }

        #endregion

        protected string GetAsseblyVersion()
        {
            return Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        protected BaseForm()
        {
            InitializeComponent();

            if (GlobalConfig.RunTimeMode)
            {
                #region Init Control
                //初始化通用公共控件repTextEdit0Dot00           
                this._repTextEdit0Dot00 = new RepositoryItemTextEdit();
                this._repTextEdit0Dot0000 = new RepositoryItemTextEdit();

                this._repTextEdit0Dot00.Name = "repTextEdit0Dot00";
                InitControl(this._repTextEdit0Dot00, "n");

                this._repTextEdit0Dot0000.Name = "repTextEdit0Dot0000";
                InitControl(this._repTextEdit0Dot0000, "n");
                _repTextEdit0Dot0000.DisplayFormat.FormatString = "0.0000";
                _repTextEdit0Dot0000.EditFormat.FormatString = "0.0000";
                #endregion

                ServerProxy = ServerProxyFactory.GetServerProxy();
                try
                {
                    _openThisFormUserId = UserSession.CurrentUser.UserId;
                }
                catch
                { }

                InitializeRepository();
            }
        }

        private void InitControl(RepositoryItemTextEdit repTextEdit, string editMask)
        {
            ((System.ComponentModel.ISupportInitialize)(repTextEdit)).BeginInit();
            repTextEdit.AutoHeight = false;
            repTextEdit.DisplayFormat.FormatString = "0.00";
            repTextEdit.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            repTextEdit.EditFormat.FormatString = "0.00";
            repTextEdit.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            //repTextEdit.Mask.EditMask = "#,##0.00;<<#,##0.00>>";
            repTextEdit.Mask.EditMask = editMask;
            repTextEdit.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            repTextEdit.Mask.UseMaskAsDisplayFormat = true;
            ((System.ComponentModel.ISupportInitialize)(repTextEdit)).EndInit();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            //存储grid的样式文件
            SaveDevExpressLayout();
        }

        private List<GridControl> GetGridControls()
        {
            var lst = new List<GridControl>();
            var ctrllst = new List<Control>();
            GetControl(ctrllst, this);
            foreach (Control item in ctrllst)
            {
                if (item is GridControl)
                {
                    lst.Add(item as GridControl);
                }
            }
            return lst;
        }

        /// <summary>
        /// 得到容器里的所有控件
        /// </summary>
        /// <param name="listCtrl"></param>
        /// <param name="ctrl"></param>
        public void GetControl(List<Control> listCtrl, Control ctrl)
        {
            try
            {
                //遍历控件
                foreach (Control ctrl1 in ctrl.Controls)
                {
                    GetControl(listCtrl, ctrl1);
                    listCtrl.Add(ctrl1);
                }
            }
            catch (Exception exception)
            {
                throw new Exception("获得控件失败 " + exception.ToString());
            }
        }

        /// <summary>
        /// 保存布局
        /// </summary>
        private void SaveDevExpressLayout()
        {
            List<GridControl> lst = GetGridControls();
            foreach (GridControl item in lst)
            {
                LayoutHelper.SaveGridLayout(item);
            }
        }

        private void LoadDevExpressLayout()
        {
            List<GridControl> lst = GetGridControls();
            foreach (GridControl item in lst)
            {
                item.Name = item.Name + GetAsseblyVersion();
                LayoutHelper.LoadGridLayout(item);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Text = this.HeadText;
            //this.LoadDevExpressLayout();
        }

        public virtual string HeadText
        {
            get;
            set;
        }

        RepositoryItemTextEdit repTextEditReadOnly = null;
        protected RepositoryItem RepTextEditReadOnly
        {
            get { return repTextEditReadOnly; }
        }

        RepositoryItemComboBox repComboBox = null;
        protected RepositoryItem RepComboBox
        {
            get { return repComboBox; }
        }

        private void InitializeRepository()
        {
            repTextEditReadOnly = new RepositoryItemTextEdit();
            repTextEditReadOnly.ReadOnly = true;
            repTextEditReadOnly.Appearance.BackColor = Color.Gray;
            repTextEditReadOnly.Appearance.BackColor2 = Color.Gray;
            repTextEditReadOnly.Appearance.Options.UseBackColor = true;

            repComboBox = new RepositoryItemComboBox();
            repComboBox.ReadOnly = false;
        }
    }
}